﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.BLL.Project;
using Jzy.Core.Common;

using Jzy.Model.Project;
using Jzy.Core.Common.Web;

using System.Collections.Generic;
using Frame.Lib.Web;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Core.Log;
using Frame.Lib.Core.Log.Operate;

public partial class Project_List : PageList
{
    private ProjectBO _prj = new ProjectBO();
    private PagerSortSetting _setting;

    #region ViewState值
    private int Status
    {
        get
        {
            if (ViewState["Status"] == null)
            {
                ViewState["Status"] = -1;
            }
            return (int)ViewState["Status"];
        }
        set
        {
            ViewState["Status"] = value;
        }
    }
    #endregion

    private int? UserID
    {
        get
        {
            if (Request["UserID"] != null)
            {
                return int.Parse(Decrypt(Request["UserID"]));
            }
            else
            {
                return null;
            }
        }
    }

    private string _encryptUserID;

    private string EncryptUserID
    {
        get
        {
            if (UserID != null)
            {
                if(_encryptUserID==null)
                {
                    _encryptUserID = Encrypt(UserID);
                }
                return _encryptUserID;
            }
            else
            {
                return null;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        _setting = this.Footer1.InitPager(new BindViewHandler(BindView));
        if (!IsPostBack)
        {
            BindStatus();
            BindYear();
            BindView();
        }

        InitSelectID(hidSelectIDs);
        JSUtil.SetTableHeight("list", 140);
        AddAttr();
    }

    private void BindYear()
    {
        _prj.BindYear(drpYear, PrjStatus.结束, PrjStatus.启动, PrjStatus.暂停);

    }

    private void AddAttr()
    {
        JSUtil.DelConfirm(btnDelete, "项目");
        JSUtil.ShowForm(lnkAdd, "增加项目", null, "Edit.aspx" , 380, 379);
        JSUtil.ClickBtn(lnkDelete, btnDelete);
    }

    private void BindStatus()
    {
        List<EnumInfo> list = ConvertUtil.ConvertEnum(typeof(PrjStatus));

        EnumInfo info = new EnumInfo();
        info.ID = -1;
        info.Name = "全部";
        list.Insert(0, info);
       
        //移除归档状态和作废状态
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i].ID == (int)PrjStatus.归档 || list[i].ID == (int)PrjStatus.作废)
            {
                list.RemoveAt(i);
                i--;
            }
        }

        list.RemoveAt(list.Count - 1);

        rptStatus.DataSource = list;
        rptStatus.DataBind();
    }

    private void BindView()
    {       
        lv.DataKeyNames = new string[] { "ID", "Status" };
        InitLV(lv, lvHeader);
        DataTable dt = null;
        btnPre.Text = btnPre.CommandArgument = (int.Parse(drpYear.SelectedValue) - 1).ToString();
        btnNext.Text = btnNext.CommandArgument = (int.Parse(drpYear.SelectedValue) + 1).ToString();

        if (int.Parse(btnPre.CommandArgument) < int.Parse(drpYear.Items[0].Value))
        {
            btnPre.Enabled = false;
        }
        else
        {
            btnPre.Enabled = true;
        }

        if (int.Parse(btnNext.CommandArgument) > int.Parse(drpYear.Items[drpYear.Items.Count - 1].Value))
        {
            btnNext.Enabled = false;
        }
        else
        {
            btnNext.Enabled = true;
        }
      
        dt = _prj.GetDataTable(drpYear.SelectedValue, Status,UserID);
       

        if (Status == -1)
        {
            pnlStatus.Visible = false;
        }
        else
        {
            pnlStatus.Visible = true;
        }

        lv.DataSource = _setting.GetDataView(dt);
        lv.DataBind();
    }
    protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        LVUtil.SetEdit(sender, e, 380, 379);
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem item = (ListViewDataItem)e.Item;
            DataRowView row = (DataRowView)item.DataItem;

            string strEncryptID = StrUtil.EncryptUrl(lv.DataKeys[item.DataItemIndex]["ID"].ToString());
            HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
            lnkName.Attributes.Remove("onclick");
            lnkName.NavigateUrl = "Task/List.aspx?PrjID=" + strEncryptID + ((UserID == null) ? "" : "&UserID=" + EncryptUserID);

            HyperLink lnkView = (HyperLink)e.Item.FindControl("lnkView");
            lnkView.NavigateUrl = "TabList.aspx?PrjID=" + strEncryptID + ((UserID == null) ? "" : "&UserID=" + EncryptUserID);

            HyperLink lnkSchedule = (HyperLink)item.FindControl("lnkSchedule");
            lnkSchedule.NavigateUrl = "TabList.aspx?PrjID=" + strEncryptID + "&TabIndex=" + (int)PrjTab.所有进展 + ((UserID == null) ? "" : "&UserID=" + EncryptUserID);

            HyperLink lnkAttach = (HyperLink)item.FindControl("lnkAttach");
            lnkAttach.NavigateUrl = "TabList.aspx?PrjID=" + strEncryptID + "&TabIndex=" + (int)PrjTab.附件 + ((UserID == null) ? "" : "&UserID=" + EncryptUserID);

            if (row["ScheduleRemark"].ToString() == "")
            {
                ((HtmlTableRow)e.Item.FindControl("trSchedule")).Visible = false;
            }
            if (row["OverseeRemark"].ToString() == "")
            {
                ((HtmlTableRow)e.Item.FindControl("trOversee")).Visible = false;
            }
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (strSelectIDs != "")
        {
            List<int> prjIDs = StrUtil.SplitToInt(strSelectIDs);
            foreach (int id in prjIDs)
            {
                ProjectInfo info = _prj.GetInfo(id);
                Logger.Operate(OperateType.Delete, "Project", "删除了项目[" + info.Name + "]");
            }

            _prj.Delete(strSelectIDs,true);
            
            BindYear();
            BindView();
        }
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindYear();
        BindView();
    }
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindView();
        
    }
    protected void rptStatus_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Status = Convert.ToInt16(e.CommandArgument);
        if (Status != -1)
        {
            lblStatus.Text = ((PrjStatus)Status).ToString();
        }
        BindView();
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        drpYear.SelectedValue = btnPre.CommandArgument;
        BindView();
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        drpYear.SelectedValue = btnNext.CommandArgument;
        BindView();
    }
}
